import {
  DATATYPE,
  REQUIRED,
  MAPPERTODB,
  VALUEMAPTYPE
} from '../enum'
export const getFieldFormItems = ({fieldList, tableName}) => {
  return [{
    prop: 'excelColumnCode',
    type: 'input',
    defaultValue: '',
    label: 'excel列编码',
    disabled: false,
    hidden: false,
    rules: [{
      required: true,
      message: '请输入excel列编码',
      trigger: 'blur'
    }]
  },
  {
    prop: 'excelColumnName',
    type: 'input',
    defaultValue: '',
    label: 'excel列名',
    disabled: false,
    hidden: false,
    rules: [{
      required: true,
      message: '请输入excel列名',
      trigger: 'blur'
    }]
  },
  {
    prop: 'orderNo',
    type: 'input',
    defaultValue: '',
    label: '排序号',
    disabled: false,
    hidden: false,
    rules: []
  },
  {
    prop: 'required',
    type: 'radio-group',
    defaultValue: false,
    label: '必填',
    disabled: false,
    hidden: false,
    rules: [{
      required: true,
      message: '请选择',
      trigger: 'change'
    }],
    options: REQUIRED
  },
  {
    prop: 'excelColumnDataType',
    type: 'select',
    defaultValue: '',
    label: 'excel列数据类型',
    disabled: false,
    hidden: false,
    rules: [{
      required: true,
      message: '请选择excel列数据类型',
      trigger: 'change'
    }],
    options: DATATYPE,
    change(formData, {
      items
    }, value) {
      const it = items.find(ele => ele.prop === 'maxLength')
      if (value === 0) {
        it && (it.disabled = false)
      } else {
        it && (it.disabled = true)
      }
    }
  },
  {
    prop: 'maxLength',
    type: 'input',
    defaultValue: '',
    label: '数据长度',
    disabled: false,
    hidden: false,
    rules: []
  },
  {
    prop: 'mapperToDb',
    type: 'radio-group',
    defaultValue: true,
    label: '是否与DB映射',
    disabled: false,
    hidden: false,
    rules: [{
      required: true,
      message: '请选择',
      trigger: 'change'
    }],
    options: MAPPERTODB,
    change(formData, {
      items
    }, value) {
      const props = ['dbFieldCode','dbFieldDataType','updatePrimaryField','valueMapType']
      items.forEach(ele => {
        if (props.includes(ele.prop)) {
          ele.hidden = value !== true
        } else if (ele.prop === 'sqlContent') {
          ele.hidden = value !== true || formData.valueMapType !== 'SQL_FETCH'
        } else if (ele.prop === 'codeType') {
          ele.hidden = value !== true || formData.valueMapType !== 'CODE_VALUE'
        } else if (ele.prop === 'pidDbFieldCode') {
          ele.hidden = value !== true || formData.valueMapType !== 'TREE_PID'
        } else if (ele.prop === 'relFieldSelect') {
          ele.hidden = value !== true || formData.valueMapType !== 'TREE_PID'
        }
      })
    }
  },
  {
    prop: 'dbFieldCode',
    type: 'tableSelect',
    defaultValue: null,
    label: 'DB字段编码',
    disabled: false,
    hidden: false,
    rules: [{
      required: true,
      message: '请选择DB字段编码',
      trigger: 'change'
    }],
    singleChoice: true,
    showName: 'fieldCode',
    primaryKey: 'fieldCode',
    columns: [{
        prop: 'fieldCode',
        label: '字段名',
        minWidth: '100'
      },
      {
        prop: 'fieldName',
        label: '描述',
        minWidth: '100'
      }
    ],
    options: [],
    change: (formData, {
      item
    }, value) => {
      if (value?.length) {
        formData.dbFieldDataType = value[0].dataType
      } else {
        formData.dbFieldDataType = null
      }
    }
  },
  {
    prop: 'dbFieldDataType',
    type: 'select',
    defaultValue: '',
    label: 'DB字段数据类型',
    disabled: true,
    hidden: false,
    rules: [],
    options: DATATYPE
  },
  {
    prop: 'updatePrimaryField',
    type: 'radio-group',
    defaultValue: false,
    label: '更新主键',
    disabled: false,
    hidden: false,
    rules: [{
      required: true,
      message: '请选择',
      trigger: 'change'
    }],
    options: REQUIRED
  },
  {
    prop: 'valueMapType',
    type: 'radio-group',
    defaultValue: 'D_MAP',
    label: '映射类型',
    width: '100%',
    disabled: false,
    hidden: true,
    rules: [{
      required: true,
      message: '请选择',
      trigger: 'change'
    }],
    options: VALUEMAPTYPE,
    change: (formData, {
      items
    }, value)=>{
      items.forEach(ele => {
        if (ele.prop === 'sqlContent') {
          ele.hidden = value !== 'SQL_FETCH'
        } else if (ele.prop === 'codeType') {
          ele.hidden = value !== 'CODE_VALUE'
        } else if (ele.prop === 'pidDbFieldCode') {
          ele.hidden = value !== 'TREE_PID'
        } else if (ele.prop === 'relFieldSelect') {
          ele.hidden = value !== 'TREE_PID'
        }
      })
    }
  },
  {
    prop: 'sqlContent',
    type: 'textarea',
    defaultValue: '',
    label: '抽取sql',
    disabled: false,
    hidden: true,
    width: '100%',
    rules: [{
      required: true,
      message: '请选择',
      trigger: 'change'
    }],
  },
  {
    prop: 'codeType',
    type: 'input',
    defaultValue: '',
    label: '字典类型',
    disabled: false,
    hidden: true,
    rules: [{
      required: true,
      message: '请选择',
      trigger: 'change'
    }],
  },
  {
    prop: 'pidDbFieldCode',
    type: 'tableSelect',
    defaultValue: null,
    label: '主键',
    disabled: false,
    hidden: true,
    rules: [{
      required: true,
      message: '请选择',
      trigger: 'change'
    }],
    singleChoice: true,
    showName: 'fieldCode',
    primaryKey: 'fieldCode',
    columns: [{
        prop: 'fieldCode',
        label: '字段名',
        minWidth: '100'
      },
      {
        prop: 'fieldName',
        label: '描述',
        minWidth: '100'
      }
    ],
    options: []
  },
  {
    prop: 'relFieldSelect',
    type: "custom",
    component: 'combination',
    width: '100%',
    hidden: true,
    rules: [],
    attrs:{
      fieldList: fieldList,
      tableName: tableName,
      defaultValue: []
    }
  }
]}
